package com.holly.unit.deform.designer.core.parser;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.holly.unit.deform.designer.enums.AdvanceElementEnum;
import com.holly.unit.deform.designer.enums.LayoutElementEnum;
import com.holly.unit.deform.designer.model.FormElement;
import com.holly.unit.deform.designer.model.Options;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

/**
 * 类描述: 表单元素迭代器
 *
 * @author fxr
 * @version 1.0.0
 * @date 2022/8/4 8:42
 */
@Slf4j
public class FormElementIterator {

    private FormElementIterator() {
        throw new UnsupportedOperationException("FormElementIterator类无法实例化");
    }

    /***
     * 从表单模板JSON中解析出所有的字段
     * @param arr 表单设计器设计JSON
     * @return List<FormElement>
     */
    public static List<FormElement> iterator(JSONArray arr) {
        List<FormElement> res = new ArrayList<>();
        // 遍历
        iterator(res,arr);
        return res;
    }

    private static void iterator(List<FormElement> list, Object obj) {

        log.info("obj class :"+obj.getClass().getSimpleName());

        // 如果是JSONArray
        if (obj instanceof JSONArray) {
            JSONArray joArray = (JSONArray) obj;

            int size = joArray.size();
            for(int i = 0 ; i < size ; i++) {
                Object json = joArray.get(i);

                iterator(list, json);
            }
        } else if (obj instanceof JSONObject) {

            JSONObject jsonObject = (JSONObject) obj;

            if(jsonObject.containsKey("model") && jsonObject.containsKey("key")  ) {
                FormElement element = jsonObject.toJavaObject(FormElement.class);
                String type = element.getType();
                if (AdvanceElementEnum.TABLE.fieldName().equals(type)) { // 子表单
                    JSONArray batchList = jsonObject.getJSONArray("tableColumns");
                    List<FormElement> childList = batchList.toJavaList(FormElement.class);
                    element.setChild(childList);
                }
                if (AdvanceElementEnum.SUBFORM.fieldName().equals(type)) {//子表单+
                    JSONArray batchList = jsonObject.getJSONArray("list");
                    List<FormElement> childList = batchList.toJavaList(FormElement.class);
                    element.setChild(childList);
                }
                list.add(element);

            } else if (jsonObject.containsKey("type") &&
                    (jsonObject.getString("type").equals(LayoutElementEnum.REPORT.fieldName()))) {// 表格布局
                FormElement element = jsonObject.toJavaObject(FormElement.class);
                // 判断显示隐藏是否有值
                Options parentOp = element.getParentOp();
                // 创建空的FormElement集合，装载table下的所有组件元素
                List<FormElement> tableChildList = new LinkedList<>();
                iterator(tableChildList,jsonObject.get("rows"));
                if (tableChildList != null && tableChildList.size()>0) {
                    tableChildList.stream().forEach(e->e.setParentOp(parentOp));
                    list.addAll(tableChildList);
                }
            } else {
                Set<String> keys = jsonObject.keySet();
                for(String key : keys) {
                    Object o = jsonObject.get(key);
                    iterator(list, o);
                }
            }
        }
    }

}
